// pages/user_end/index/index.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({

  /** 
   * 页面的初始数据
   */
  data: {
	  countdown: 5,
    banner: [],
	articlelist:[],
    icon: [],
    category: [],
    token: '',
    show: false,
    keyword: '',
    cityId: 802,
    cityName: "",
    unreadnum: 0,
    bg: '#08B266',
	pxlist:[],
  pxtimelist:[],
  agreement:false,
  buttonDisabled:true,
  expertlist:[],
   userinfo: ''
  },
  go(e){
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
      wx.navigateTo({
        url:e.currentTarget.dataset.url,
      })
  },
  goArticle(e){
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
	  var id = e.currentTarget.dataset.id;
	  
      wx.navigateTo({
        url: '/pages/user_end/article/article?id=' + id,
      })
  },
  countdownTimer: function() {
    const that = this;
    let countdown = that.data.countdown;  
    // 设置定时器
    let timer = setInterval(function() {
      countdown--;	    
  	that.setData({
  	  countdown: countdown
  	});
  	console.log(that.data.countdown);
      // 当倒计时结束时
      if (countdown <= 0) {
        clearInterval(timer);
       that.setData({
         countdown: 0,
         buttonDisabled: false
       });
      }
    }, 1000); 
  },
  goArticleDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/user_end/article/article?id=' + id,
    })
  },
  goProductList(e){
  	  wx.requestSubscribeMessage({
      tmplIds: [
  	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
  	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
  	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
  	    ],
  	    success: (res) => {
  	    },
  	    fail: (res) => {
  	    },
  	  })
  	  var id = e.currentTarget.dataset.id;
  	  
      wx.navigateTo({
        url: '/pages/user_end/product_list/product_list?id=' + id,
      })
  },
  goExpertList(e)
  {
	  wx.requestSubscribeMessage({
	  tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
	  var id = e.currentTarget.dataset.id;
	  
	  wx.navigateTo({
	    url: '/pages/user_end/expert_list/expert_list?id=' + id,
	  })
  },
  goform(){
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
    wx.navigateTo({
      url: '/pages/server_end/server_start/server_start',
    })
  },
  changekeyword(e){
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
     var that =  this;
     that.setData({
         keyword:e.detail.value
     })
  },
  close() {
    this.setData({
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
	//  that.getLocation();
    that.checkUserIsMaster();
    that.getBanner();			
    that.getCategory();
    that.getArticle();
     that.getshoplist();
     that.getpxlist();
    that.getexpertlist();
    that.getIcon();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
	 
    var that = this;
	
    cookie.getCookie('cityId', function (res) {
	
      if (res) {
         that.setData({
            cityId:res
         })
         cookie.getCookie('cityName', function (res) {
              that.setData({
                cityName:res
            })
         })
		
      }else{
        that.getLocation();
      }
    })
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        that.getuserinfo();
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.getUnread();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getBack(){
    wx.exitMiniProgram({
      success: function (res) {
        wx.showToast({
          title: '退出成功',
          icon:'none',
          duration:2000
        })
      }	
    })
  },
  getuserinfo()
  {
	  var that = this;
	  let param = {
	    scoure: 'wxapp',
	    token: that.data.token
	  }
	  
	  wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
	  request.request(url.getuserinfo, param, url.REQUEST_METHOD_GET).then((res) => {
	    wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画	
	    if (res.data.code == 1) {
			that.setData({
			  userinfo: res.data.data
			})
	        if(res.data.data.privacy==0)
			{
				this.setData({
				  agreement:true
				})
				that.countdownTimer();
			}
			var groupid = res.data.data.group_id;		
			if(groupid==2)
			{
				if (that.data.userinfo.group_id == 2 && that.data.userinfo.checkinfo == 1) {
				  wx.setStorageSync('lastModePage', 2)
				  wx.requestSubscribeMessage({
				    tmplIds: [
				     'q9Js-vQrnKFfxtsIYMrYF6CQgNNsPITiZtcznzk8P-M',
				     'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao',		   
				     'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0'
				    ],
				    success: (res) => {
				      wx.navigateTo({
				        url: '/pages/user_end/index_sf/index_sf'
				      })
				    },
				    fail: (res) => {
				      wx.navigateTo({
				        url: '/pages/user_end/index_sf/index_sf'
				      })
				    },
				  })				
				}
			}
			if(groupid==5)
			{
			
				if (that.data.userinfo.group_id == 5 && that.data.userinfo.checkinfo == 1) {
				  wx.setStorageSync('lastModePage', 5);
		
						wx.navigateTo({
						  url: '/pages/user_end/index_zj/index_zj'
						})
					
				}
			}			
			
			console.log('agreement:'+that.data.agreement);
			console.log('privacy:'+res.data.privacy);
	    }
	  })
  },
  getConsent(){
	  var that = this;
	  let param = {
	    scoure: 'wxapp',
	    token: that.data.token
	  }
	  wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
	  request.request(url.agreement, param, url.REQUEST_METHOD_GET).then((res) => {
	    wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
	     console.log(res)
	    if (res.data.code == 1) {
			this.setData({
			  agreement:false
			})
	      console.log(res.data.data)
	    }
	  })
    
  },
  goCity(){
    wx.navigateTo({
      url:"../../choose_city/choose_city"
    })
  },
  getshoplist() {
    var that = this;
    let param = {
      scoure: 'wxapp'
    }
	console.log('getshiolist：'+url.getshoplist);
	
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.shoplist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
	   console.log(res)
      if (res.data.code == 1) {
        console.log(res.data.data)
        that.setData({
          shoplist: res.data.data
        })
      }
    })
  },
  getexpertlist() {
    var that = this;
    let param = {
      scoure: 'wxapp'	
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.secondexpertcategorylist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
  	   console.log(res)
      if (res.data.code == 1) {
        console.log(res.data.data)
        that.setData({
          expertlist: res.data.data
        })
      }
    })
  },
  getLocation(){
    var that = this;
	wx.getFuzzyLocation({
				type: 'wgs84',
				success(res) {
				var latitude = res.latitude;
				var longitude = res.longitude;
				 wx.setStorageSync('latitude',latitude);
				 wx.setStorageSync('longitude',longitude);
				 that.getCity();
				},
				fail(err) {
					console.error(err);
				}
			})
  },
  getCity(){
    var that = this;
    var lat = wx.getStorageSync('latitude');
    var lng = wx.getStorageSync('longitude');
	console.log(lat);
	console.log(lng);
    if(lat){
      let param = {
        scoure: 'wxapp',
        lat:lat,
        lng:lng
      }
      wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
      request.request(url.getlocation, param, url.REQUEST_METHOD_POST).then((res) => {
        wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
        if(res.data.code==1){
            that.setData({
              cityId: res.data.data.id,
              cityName:res.data.data.name
            })
            wx.setStorageSync('cityId',res.data.data.id);
            wx.setStorageSync('cityName',res.data.data.name);
			console.log('=================');
			
			console.log(res.data.data);
			// that.getIcon();
       }
      })  
    }			
  },
  getUnread(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getunread, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
          that.setData({
            unreadnum:res.data.data
          })
      }
    })  			
  },
  doSearch() {
    var that = this;
    if(!that.data.keyword){
       wx.showToast({
         title: '请输入搜索内容',
         icon:'none'
       })
       return false;
    }
    wx.navigateTo({
      url: '../server_list/server_list?id=0&type=1&key=' + that.data.keyword
    })
  },
  gotoMsg() {
	wx.requestSubscribeMessage({
    tmplIds: [
      'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
      'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
      'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
    ],
	  success: (res) => {
	  },
	  fail: (res) => {
	  },
	})
    wx.navigateTo({
      url: '../message/message',
    })
  },
  gotoSeviceList(e) {
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
  var id =e.currentTarget.dataset.id;
  var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../server_list/server_list?id=' + id + "&type=" + type+"&key="
    })
  },
  gotoSeviceDetail(id) {
	  wx.requestSubscribeMessage({
	    tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
    wx.navigateTo({
      url: '../server_info/server_info?id=' + id
    })
  },
  clickBanner(e) {	  
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
    console.log(e);
    var jump = e.currentTarget.dataset.jump;
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    if(jump=='1'){
      if(type=='1'){
        wx.navigateTo({
          url:"../article/article?id="+id
        })
      }else if(type=='2'){
        wx.navigateTo({
          url:"../server_info/server_info?id="+id
        })						
      }
    }
  },
  getBanner() {
    var that = this;
    let param = {
      scoure: 'wxapp',
      location: 1
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getbannerlist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
        that.setData({
          banner:res.data.data
        })
      }
    })  			
  },
  getIcon() {
    var that = this;
    let param = {
      scoure: 'wxapp',
	  cityName:that.data.cityName
	  
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getcategorylist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
        that.setData({
          icon:res.data.data
        })
      }
    })  	
  },
  getArticle() {
    var that = this;
    let param = {
      scoure: 'wxapp'
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.articlelist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
        that.setData({
          articlelist:res.data.data
        })
      }
    })  	
  },
  getCategory() {
    var that = this;
    let param = {
      scoure: 'wxapp',
	  cityName:that.data.cityName
	  
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getservicecatgory, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
        that.setData({
          category:res.data.data
        })
      }
    })  	
  },
  checkUserIsMaster(){

    var lastModePage = wx.getStorageSync('lastModePage');
    if(lastModePage && lastModePage == 2) {
      // wx.navigateTo({
      //   url: '../../server_end/index/index'
      // })
    }
  },
  gototrain(e){
  
  var id =e.currentTarget.dataset.id;
  wx.navigateTo({
    url:"../train/train?id="+id
  })
  },
  getpxlist(){
	  var that = this;
	  let param = {
	    scoure: 'wxapp'
	  }
	  wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
	  request.request(url.getpxlist, param, url.REQUEST_METHOD_GET).then((res) => {
	    wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
	    if(res.data.code==1){
	      that.setData({
	        pxlist:res.data.data
	      })
	    }
	  })  	
  },
  getpxtime(e){
  	  var that = this;
	  var id = e.currentTarget.dataset.id;
	
	  
  	  let param = {
  	    scoure: 'wxapp',
		id:id
  	  }
  	  wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
  	  request.request(url.getpxtime, param, url.REQUEST_METHOD_GET).then((res) => {
  	    wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画

		
		
  	    if(res.data.code==1){
		   const contentString = res.data.data.join('\r\n');
		
		  wx.showModal({
		    // title: '提示',
		    content: contentString,
		    showCancel: false, // 是否显示取消按钮，默认为 true
		    confirmText: '确定', // 确定按钮的文字，默认为"确定"
		    confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为"#3CC51F"
		    success (res) {
		      if (res.confirm) {
		        console.log('用户点击确定')
		      } else if (res.cancel) {
		        console.log('用户点击取消')
		      }
		    }
		  });
  	    }
  	  })  	
  },
})